package cn.yunyichina.provider.pay.service;

import cn.yunyichina.provider.iface.entity.dto.pacity.PacityOrderQueryRequestDTO;
import cn.yunyichina.provider.pay.entity.PaymentSetting;
import cn.yunyichina.utils.commons.RandomUtil;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Created by Lullaby on 2017/5/18.
 */
public class BaseOrderService extends BaseService {

    protected boolean checkOrderQueryParameters(Map<String, String> params) {
        if (StringUtils.isEmpty(params.get("app_id"))) return false;
        if (StringUtils.isEmpty(params.get("order_no")) && StringUtils.isEmpty(params.get("pay_order_no")))
            return false;
        return true;
    }

    protected boolean checkOrderReverseParameters(Map<String, String> params) {
        if (StringUtils.isEmpty(params.get("app_id"))) return false;
        if (StringUtils.isEmpty(params.get("order_no")) && StringUtils.isEmpty(params.get("pay_order_no")))
            return false;
        return true;
    }

    protected PacityOrderQueryRequestDTO buildQueryPacityOrderRequestDTO(Map<String, String> params) throws Exception {
        String appId = params.get("app_id");
        PaymentSetting paymentSetting = getPaymentSetting(appId);

        PacityOrderQueryRequestDTO requestDTO = new PacityOrderQueryRequestDTO();
        requestDTO.setAccessToken(getPacityAccessToken());
        requestDTO.setPublicKey(paymentSetting.getPayPublicKey());
        requestDTO.setPrivateKey(paymentSetting.getPrivateKey());

        requestDTO.setPlatMchId(paymentSetting.getMerchantAccount());
        requestDTO.setNonceStr(RandomUtil.getRandomALLChar(32));
        requestDTO.setPlatMchTradeNo(params.get("order_no"));

        return requestDTO;
    }

}
